package cn.itcast.demo;

import cn.itcast.demo.pojo.Heima55;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.junit.Test;

import java.util.List;

public class SolrQueryIndexTest {


    @Test
    public void testQueryIndex() throws SolrServerException {
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");


        //封装查询条件对象
        SolrQuery query =new SolrQuery("title:手机");

        //执行查询，传入查询条件，并执行查询得到响应
        QueryResponse queryResponse = solrServer.query(query);

        //解析查询的结果
        SolrDocumentList results = queryResponse.getResults();

        results.forEach(result->{
            String id = (String) result.get("id");
            String title = (String) result.get("title");
            Long price = Long.valueOf(result.get("price").toString());

            System.out.println("id:"+id+" title:"+title+" price:"+price);
        });

    }


    @Test
    public void testQueryIndexWithBean() throws SolrServerException {
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");


        //封装查询条件对象
        SolrQuery query =new SolrQuery("title:手机");

        //执行查询，传入查询条件，并执行查询得到响应
        QueryResponse queryResponse = solrServer.query(query);

       //解析查询结果
        List<Heima55> beans = queryResponse.getBeans(Heima55.class);

        beans.forEach(bean->{
            String id = bean.getId();
            String title = bean.getTitle();
            Long price = bean.getPrice();

            System.out.println("id:"+id+" title:"+title+" price:"+price);
        });

    }

}
